const SpiderBaseClass = require('./SpiderBaseClass');

const listSize = 60;//列表页面有多少个主播信息

class SpiderKuaiShou extends SpiderBaseClass {

  static firstListPage() {
    return 1;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {
    const res = await SpiderKuaiShou.post(SpiderKuaiShou.listPageUrl(),
        {
          dataType: 'json',
          headers: {
            Origin: 'https://live.kuaishou.com',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Content-Type': 'application/json;charset=UTF-8',
            'Accept': 'application/json',
            'Cache-Control': 'no-cache',
          },
          data: {currentPage: page, pageSize: listSize, type: 'DQRM'},
        });

    const list = res.data.list.map(room => {
      let gender = 0;
      if (room.user.sex) {
        room.user.sex = room.user.sex.toLowerCase();
        if (room.user.sex === 'm')
          gender = 1;
        else if (room.user.sex === 'f')
          gender = 2;
      }

      const watching = room.watchingCount.toLowerCase();
      let num_watch = parseFloat(watching);
      if (isNaN(num_watch))
        num_watch = 0;
      else if (watching.indexOf('w') !== -1)
        num_watch *= 10000;

      return {
        anchor: {
          room: room.user.id,
          name: room.user.name,
          gender,
          avatar: room.user.profile,
        },
        type: room.gameName,
        data: {
          num_watch,
          num_live: 0,
          num_follower: 0,
          num_gift: 0,
        },
      };
    });
    return {
      list,
      page,
      nextPage: page + 1,
      hasNextPage: page < res.data.totalPage,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
  }

  /**
   * 生成列表页面的网址
   * @return string
   */
  static listPageUrl(page) {
    return 'https://live.kuaishou.com/liveStream/liveCardList';
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
  }

}

module.exports = SpiderKuaiShou;